<?php
require_once 'kernel/form.php';
class Comments{
	private $tplVarName = "body";
	private $emailRequired = false;
	private $useCaptcha = true;
	
	private $contentId;
	private $page;
	
	private $formFields = array();
	
	public function __construct($varName,$emailRequired,$contentId,$page,$useCaptcha = true) {
		$this->tplVarName = $varName;
		$this->emailRequired = $emailRequired;
		$this->useCaptcha = $useCaptcha;
		$this->contentId = $contentId;
		$this->page = $page;
		
		
		$this->formFields = $this->getFormArray();
	}
	
	public function getFormArray() {
		$formField = array();
		$i = 0;
		$formField[$i]['fieldName'] = "Name";
		$formField[$i]['fieldDescription'] = "Bitte geben Sie ihren Namen ein.";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXT;
		$formField[$i]['fieldDefaultValue'] = "";
		$formField[$i]['fieldCheck'] = true;
		$formField[$i]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		$i++;
		
		$formField[$i]['fieldName'] = "Email";
		$formField[$i]['fieldDescription'] = "Ihre Email-Adresse";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXT;
		$formField[$i]['fieldDefaultValue'] = "";
		$formField[$i]['fieldCheck'] = $this->emailRequired;
		$formField[$i]['fieldCheckExtra'] = Form::$CHECK_TEXT_EMAIL;
		$i++;
		
		$formField[$i]['fieldName'] = "Titel";
		$formField[$i]['fieldDescription'] = "Der Titel ihres Kommentars";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXT;
		$formField[$i]['fieldDefaultValue'] = "";
		$formField[$i]['fieldCheck'] = true;
		$formField[$i]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		$i++;
		
		$formField[$i]['fieldName'] = "Kommentar";
		$formField[$i]['formName'] = "text";
		$formField[$i]['fieldDescription'] = "Ihr Kommentar.";
		$formField[$i]['fieldDisplayBBCodeSmilies'] = true;
		$formField[$i]['fieldSelectOptions'] = "50_10";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXTAREA;
		$formField[$i]['fieldDefaultValue'] = "";
		$formField[$i]['fieldCheck'] = true;
		$formField[$i]['fieldCheckExtra'] = Form::$CHECK_TEXTAREA_NOTEMPTY;
		$i++;
		
		return $formField;
	}
	
	public function output() {
		
		$form = new Form();
		$form->initByData("Kommentar schreiben",true,"Absenden!",$this->formFields,true,true,$this->useCaptcha);
		
		// Done?
		$check = $form->completeCheck();
		if($check === "preview") {
			// Preview
			$cp = array();
			$cp['comTitle'] = Badwords::getInstance()->parse(DB::parseVar(htmlspecialchars($_POST['Titel'])));
			$cp['comAuthorEmail'] = DB::parseVar($_POST['Email']);
			$cp['comAuthor'] = DB::parseVar($_POST['Name']);
			$cp['formatDate'] = General::formatDate(time());
			
			$cp['parsedText'] = nl2br(DB::parseVar(htmlspecialchars($_POST['text'])));
			$cp['parsedText'] = BBCode::getInstance()->parse($cp['parsedText']);
			$cp['parsedText'] = Smilies::getInstance()->parse($cp['parsedText']);
			$cp['parsedText'] = Badwords::getInstance()->parse($cp['parsedText']);
			
			General::getTpl()->assign('cp',$cp);
			General::getTpl()->assign('preview',
				General::getTpl()->fetch('commentsPreview.html'));
		
			$form->outputFormular($this->tplVarName,'formular.html');
		}elseif($check) {
			
			// Save Comment
			DB::getInstance()->query("
				INSERT INTO
					".Tables::$commentsTbl."
				SET
					comConId = '".DB::parseVar($this->contentId)."',
					comPage = '".DB::parseVar($this->page)."',
					comTitle = '".DB::parseVar(htmlspecialchars($_POST['Titel']))."',
					comText = '".DB::parseVar(htmlspecialchars($_POST['text']))."',
					comAuthor = '".DB::parseVar($_POST['Name'])."',
					comAuthorEmail = '".DB::parseVar($_POST['Email'])."',
					comDate = '".time()."'");
			
			// Save Data
			General::ouputMessage('Erfolgreich','Der Kommentar wurden erfolgreich gespeichert.',"comMsg");
		}else{
			$form->outputFormular($this->tplVarName,'formular.html');
		}
	}
	
	public function getComments() {
		$query = DB::getInstance()->query("
			SELECT
				comTitle,
				comText,
				comAuthor,
				comAuthorEmail,
				comDate
			FROM
				".Tables::$commentsTbl."
			WHERE
				comConId = '".DB::parseVar($this->contentId)."' &&
				comPage = '".DB::parseVar($this->page)."'
			ORDER BY
				comDate DESC");
		$comments = array();
		while($c = DB::getInstance()->fetchAssoc($query)) {
			$c['formatDate'] = General::formatDate($c['comDate']);
			$c['parsedText'] = nl2br($c['comText']);
			
			$c['parsedText'] = BBCode::getInstance()->parse($c['parsedText']);
			$c['parsedText'] = Smilies::getInstance()->parse($c['parsedText']);
			$c['parsedText'] = Badwords::getInstance()->parse($c['parsedText']);
			$c['comTitle'] = Badwords::getInstance()->parse($c['comTitle']);
			$comments[] = $c;
		}
		return $comments;
	}
	
	public function outputComment() {
		$comments = $this->getComments();
		General::getTpl()->assign('comments',$comments);
		
		General::getTpl()->assign('commentsOutput',
			General::getTpl()->fetch("commentsOutput.html"));
	}
	
	
	public static function getCommentsMin($conId,$page,$min = "",$max = "") {
		if(!is_numeric($conId)) exit;
		
		$limitQuery = "";
		if($min = "" && $max = "") {
			$limitQuery = "LIMIT ".$min.",".$max;
		}
		
		$query = DB::getInstance()->query("
			SELECT
				id,
				comTitle,
				comText,
				comAuthor,
				comAuthorEmail,
				comDate
			FROM
				".Tables::$commentsTbl."
			WHERE
				comConId = '".DB::parseVar($conId)."' &&
				comPage = '".DB::parseVar($page)."'
			ORDER BY
				comDate DESC
			".$limitQuery);
		$comments = array();
		while($c = DB::getInstance()->fetchAssoc($query)) {
			$c['date'] = General::formatDate($c['comDate']);
			$comments[] = $c;
		}
		return $comments;
	}
}
?>